package com.zmn.oms.admin.controller.purpose;

import com.google.common.collect.Maps;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.data.LayuiTableDO;
import com.zmn.common.utils.office.excel.ExportExcel;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.business.interfaces.purpose.OrderPurposeBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.model.entity.purpose.OrderPurpose;
import com.zmn.oms.model.entity.purpose.OrderPurposeQuery;
import com.zmn.oms.model.vo.purpose.OrderPurposeVO;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

/**
 * 类描述: 意向单
 *
 * @author yangjiangming
 * @date 2019-04-29 10:09
 */
@Controller
@RequestMapping("/order/purpose")
public class PurposeListController extends OmsBaseController {

    @Autowired
    private OrderPurposeBService orderPurposeBService;


    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("purpose/list/list");

        // 是否
        mv.addObject("yesOrNo", OrderConsts.getYesOrNoObj());

        // 服务分类 common
        mv.addObject("servCategList", super.getShowServCategList());

        return mv;
    }

    @RequestMapping("/ajax/list")
    @ResponseBody
    public ResultDTO ajaxPurposeList(@RequestBody OrderPurposeQuery query) {
        List<OrderPurposeVO> orderPurposeVOList = orderPurposeBService.listOrderPurposeByQuery(query);
        LayuiTableDO layuiTableDO = new LayuiTableDO();
        layuiTableDO.setCount(query.getTotalCount());
        layuiTableDO.setData(orderPurposeVOList);
        return ResultDTO.success(layuiTableDO);
    }

    @RequestMapping("/list/export")
    @ResponseBody
    public void ajaxListExport(OrderPurposeQuery query, HttpServletResponse response) throws Exception {

        query.setPageSize(5000);
        List<OrderPurposeVO> voList = orderPurposeBService.exportListOrderPurposeByQuery(query);

        LinkedHashMap<String, String> fieldMap = Maps.newLinkedHashMap();
        fieldMap.put("receiveTime", "下单时间");
        fieldMap.put("userInfoCallBack", "用户信息");
        fieldMap.put("addressCallBack", "地址");
        fieldMap.put("servCategName", "服务分类");
        fieldMap.put("productInfo", "服务信息");
        fieldMap.put("creater", "录入人");
        fieldMap.put("saleStatusCallBack", "是否售卖");

        ExportExcel<OrderPurposeVO> excel = new ExportExcel<>(voList, fieldMap, true);

        excel.setCallback((fieldName, entity) -> {
            switch (fieldName) {
                case "userInfoCallBack":
                    return StringUtils.defaultString(entity.getUsername()) + ":" + StringUtils.defaultString(entity.getUserPhone());
                case "addressCallBack":
                    return StringUtils.defaultString(entity.getProvinceName()) + " " + StringUtils.defaultString(entity.getCityName()) + " " + StringUtils.defaultString(entity.getUserStreet());
                case "saleStatusCallBack":
                    return entity.getSaleStatus() == 2 ? "是" : "否";
            }
            return null;
        });
        excel.exportExcel(response, "意向单");
    }

    @RequestMapping("/operate/all/permission")
    @ResponseBody
    public ResultDTO ajaxAllPermission(Integer orderPurposeId, Integer type) {
        List<OperatePermissionVO> voList = getOrderPurposePermission(orderPurposeId, type);


        return ResultDTO.success(voList);
    }

    @RequestMapping("/operate/one/permission")
    @ResponseBody
    public ResultDTO ajaxOnePermission(Integer orderPurposeId, Integer type) {
        List<OperatePermissionVO> voList = getOrderPurposePermission(orderPurposeId, type);

        return ResultDTO.success(type == 11 ? voList.get(0) : voList.get(1));
    }

    private List<OperatePermissionVO> getOrderPurposePermission(Integer orderPurposeId, Integer type) {

        OrderPurpose orderPurpose = orderPurposeBService.findOrderPurposeByKey(orderPurposeId);

        List<OperatePermissionVO> voList = new ArrayList<>(2);
        OperatePermissionVO vo = new OperatePermissionVO();
        vo.setId(11);
        vo.setName("售卖");
        vo.setCode(200);
        vo.setCanOperate(false);

        if (!Objects.equals(orderPurpose.getSaleStatus(), 2)) {
            vo.setCanOperate(true);
        }

        if (!vo.getCanOperate()) {
            vo.setMsg("该意向单已为售卖状态");
        }
        voList.add(vo);

        vo = new OperatePermissionVO();
        vo.setId(12);
        vo.setName("下单");
        vo.setCode(200);

        vo.setCanOperate(true);
        if (Objects.equals(orderPurpose.getIsCreateOrder(), GlobalConsts.YES)) {
            vo.setCanOperate(false);
            vo.setMsg("该意向单已下过单,勿重复下单");
        }
        voList.add(vo);

        return voList;
    }
}
